package com.hejia.bulbul.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.Announcement;
import com.hejia.alauda.service.AnnouncementService;
import com.hejia.alauda.utils.HttpResponse;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;

/**
 * 公告控制器
 * 
 * @author hyr
 *
 */
@Controller
@RequestMapping("announcements")
public class AnnouncementController extends BaseController {

	private static final Log logger = LogFactory.getLog(AnnouncementController.class);

	@Resource
	private AnnouncementService announcementService;

	@RequestMapping
	public String list(Model model, Pager<Announcement> pager, String title) {

		pager = this.announcementService.findAnnouncements(pager, title);
		model.addAttribute("title", title);
		model.addAttribute("announcements", pager);

		// 分页
		PagerHelper<Announcement> helper = new PagerHelper<>();
		helper.setPager(pager);
		Map<String, String> map = new HashMap<>();
		if (title != null) {
			map.put("title", title);
		}
		helper.setParams(map);
		helper.setBaseUrl("/announcements");
		model.addAttribute("pagerHelper", helper);
		
		return "announcement/list";
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse add(Announcement announcement) {

		HttpResponse response = new HttpResponse();

		try {
			announcement = announcementService.addAnnouncement(announcement);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("添加公告成功");
			response.setData(announcement);

		} catch (Exception e) {
			logger.error("添加公告失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("添加公告失败 " + e.getMessage());
			response.setData(announcement);
		}

		return response;
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public HttpResponse delete(@PathVariable Long id) {

		HttpResponse response = new HttpResponse();

		try {
			announcementService.deleteAnnouncement(id);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("删除公告成功");

		} catch (Exception e) {
			logger.error("删除公告失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("删除公告失败 " + e.getMessage());
		}

		return response;
	}
	
	@RequestMapping("{id}/edit")
	public String edit(Model model, @PathVariable Long id) {
		
		Announcement announcement = this.announcementService.findAnnouncementById(id);
		model.addAttribute("announcement", announcement);
		
		return "announcement/edit";
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse update(@PathVariable Long id, Announcement announcement) {

		HttpResponse response = new HttpResponse();
		
		try {
			this.announcementService.updateAnnouncement(announcement);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setData(announcement);

		} catch (Exception e) {
			logger.error("公告更新失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("公告更新失败 " + e.getMessage());
		}

		return response;
	}

}
